package com.sky.financelake.bean.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.sky.financelake.bean.db.Combination;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class DBManager {
    private final static String dbName = "FINANCE_DB";
    private static DBManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private GreenDaoContext mContext;
    private DaoSession daoSession;

    public DBManager(Context context) {
        mContext = new GreenDaoContext(context);
        openHelper = new DaoMaster.DevOpenHelper(mContext, dbName, null);
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        daoSession = daoMaster.newSession();
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static DBManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (DBManager.class) {
                if (mInstance == null) {
                    mInstance = new DBManager(context);
                }
            }
        }
        return mInstance;
    }


    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(mContext, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(mContext, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

    /**
     * 插入一条记录
     *
     * @param combination 机器信息
     */
    public void insertCombination(Combination combination) throws Exception {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        CombinationDao combinationDao = daoSession.getCombinationDao();
        QueryBuilder<Combination> qb = combinationDao.queryBuilder();
        List<Combination> list = qb.list();
        if (list.size() > 5) {
            combinationDao.delete(list.get(0));
        }
        combinationDao.insert(combination);
    }


    /**
     * 删除一条记录
     *
     * @param machine
     */
    public void deleteCombination(Combination combination) throws Exception {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        CombinationDao combinationDao = daoSession.getCombinationDao();
        combinationDao.delete(combination);
    }


    /**
     * 查询用户列表
     */
    public List<Combination> queryCombinationList() throws Exception {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        CombinationDao combinationDao = daoSession.getCombinationDao();
        QueryBuilder<Combination> qb = combinationDao.queryBuilder().orderDesc(CombinationDao.Properties.Id);
        List<Combination> list = qb.list();
        return list;
    }


    /**
     * 更新一条记录
     *
     * @param machine
     */
    public void updateCombination(Combination combination) throws Exception {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        CombinationDao combinationDao = daoSession.getCombinationDao();
        combinationDao.update(combination);
    }

}
